package com.silence.gankio.util

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.Observer

object PermissionUtil {

    private var rxPermissions: RxPermissions? = null

    fun with(activity: FragmentActivity): PermissionUtil {
        rxPermissions = RxPermissions(activity)
        return this
    }

    fun with(fragment: Fragment): PermissionUtil {
        rxPermissions = RxPermissions(fragment)
        return this
    }

    /**
     * @param type Manifest.permission
     */
    fun check(type: String, observer: (t: Boolean) -> Unit) {
        rxPermissions?.request(type)
            ?.subscribe(observer)
    }
}